package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmpReportingMethod;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpReportingMethodForm;
import com.edu.hre.struts.form.pim.EmpReportingMethodListForm;

public class EmpReportingMethodDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empReportingMethodDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public ArrayList<EmpReportingMethodForm> getReportingMethodListInfo(int limit, int offset){

		List<HreEmpReportingMethod> hreReportingMethods = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreEmpReportingMethod.class);
		ArrayList<EmpReportingMethodForm> reportingMethods = new ArrayList<EmpReportingMethodForm>();
		
		if (hreReportingMethods == null) {
			return reportingMethods;
		}
		for (HreEmpReportingMethod hreMet: hreReportingMethods) {
			
			try {

				EmpReportingMethodForm reportingMethod = new EmpReportingMethodForm();
				reportingMethod.setId(hreMet.getReportingMethodId());
				reportingMethod.setName(CommonUtils.normalizeString(hreMet.getReportingMethodName()));
	
				reportingMethods.add(reportingMethod);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}

		return reportingMethods;
	}

	public int countReportingMethods(){

		int count = baseService.countByColumns(null, null, null, "id", HreEmpReportingMethod.class);
		return count < 0 ? 0 : count;
	}

	public void deleteReportingMethods(EmpReportingMethodListForm form){

		ArrayList<EmpReportingMethodForm> info = (ArrayList<EmpReportingMethodForm>) form.getAllMethods();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				EmpReportingMethodForm method = info.get(i);
				if (method.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, method.getId(), null, null, 0, HreEmpReportingMethod.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
